﻿using ProductMonitor.Command;
using ProductMonitor.MyUserControl;
using ProductMonitor.ViewModels;
using ProductMonitor.Views;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowVM();
            var viewModel = DataContext as MainWindowVM;
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        if (viewModel != null)
                        {
                            viewModel.TimeStr = DateTime.Now.ToString("HH:mm:ss");
                            if(DateTime.Now.TimeOfDay.TotalSeconds > 1 && DateTime.Now.TimeOfDay.TotalSeconds < 10)
                            {
                                viewModel.WeekDay = DateTime.Now.DayOfWeek > 0 ? Convert.ToInt16(DateTime.Now.DayOfWeek).ToString() : Convert.ToInt16(DateTime.Now.DayOfWeek + 7).ToString();
                                viewModel.MounthDay = DateTime.Now.ToString("yyyy-MM-dd");
                            }
                        }
                    });
                }
            });
        }

        private void BtnMin(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void BtnSizeToggel(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                // 如果当前是最大化状态，切换到正常状态
                this.WindowState = WindowState.Normal;

            }
            else
            {
                // 如果当前是正常状态，切换到最大化状态
                this.WindowState = WindowState.Maximized;
            }
        }

        private void BtnClose(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        #region 弹出配置窗口

        private RelayCommand _showSettingWinCmd;

        public RelayCommand ShowSettingWinCmd
        {
            get
            {
                if (_showSettingWinCmd == null)
                {
                    _showSettingWinCmd = new RelayCommand(ShowSettingWin);
                }
                return _showSettingWinCmd;
            }
            set { _showSettingWinCmd = value; }
        }

        private void ShowSettingWin()
        {
            SettingsWindow settingsWindow = new SettingsWindow() { Owner = this };
            settingsWindow.ShowDialog();
        }

        #endregion
    }
}